/**
 * 
 */
package wong.board.client.widgets;

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.InputElement;
import com.google.gwt.user.client.ui.HasText;

/**
 * @author wong
 * 
 */
public class Input extends Component implements HasText {
	private InputElement inputElement;
	private DataList dataList;

	public Input() {
		this.setStyleName(css.input());
	}

	@Override
	protected Element createElement() {
		inputElement = Document.get().createTextInputElement();
		return inputElement;
	}

	@Override
	public String getText() {
		return inputElement.getValue();
	}

	@Override
	public void setText(String text) {
		inputElement.setValue(text);
	}

	public void setPlaceholder(String placeholder) {
		this.getElement().setAttribute("placeholder", placeholder);
	}

	public void setList(String listId) {
		this.getElement().setAttribute("list", listId);
		if (this.dataList == null || !this.dataList.getId().equals(listId)) {
			this.dataList = new DataList(listId);
		}
	}

	public DataList getDataList() {
		return this.dataList;
	}

	public void resetList() {
		this.getElement().removeAttribute("list");
		this.dataList.removeFromParent();
		this.dataList = null;
	}

	public void setSize(int size) {
		this.getElement().setAttribute("size", Integer.toString(size));
	}

}
